package com.ugrow.ugrowcommunity.activity.courseselection;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ugrow.ugrowcommunity.BuildConfig;
import com.ugrow.ugrowcommunity.activity.courseselection.adapter.CourseAdapter;
import com.ugrow.ugrowcommunity.activity.courseselection.bean.CourseBean;
import com.ugrow.ugrowcommunity.activity.courseselection.data.CourseTchBean;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.ActivityCourseSelectionBinding;
import com.ugrow.ugrowcommunity.util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;

public class CourseSelectionActivity extends AppCompatActivity {

    private ActivityCourseSelectionBinding binding;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setTheme(R.style.WhiteTheme);
        getWindow().setStatusBarColor(0xffffff);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = ActivityCourseSelectionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.name.setText(Data.StudentBean.getData().get(0).getStuName());
        binding.major.setText(Data.StudentBean.getData().get(0).getMajor());
        binding.number.setText(Data.StudentBean.getData().get(0).getStuIdCard());
        binding.college.setText(Data.StudentBean.getData().get(0).getFaculty());
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        HashMap<String, Object> map = new HashMap<>();
        map.put("stuNum",Data.StuNum);
        HttpUtil.httpPost(BuildConfig.COURSE_SELECTION, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
            @Override
            public void success(HttpUtil.Result result) {
                Log.d("TAG",result.content);
                final CourseBean courseBean = new Gson().fromJson(result.content, CourseBean.class);
                CourseAdapter courseAdapter = new CourseAdapter(CourseSelectionActivity.this,courseBean.getData());
                LinearLayoutManager layoutManager = new LinearLayoutManager(CourseSelectionActivity.this);
                binding.recyclerview.setLayoutManager(layoutManager);
                binding.recyclerview.setAdapter(courseAdapter);
                courseAdapter.setOnItemClick(new CourseAdapter.OnItemClick() {
                    @Override
                    public void onClick(TextView selection,int position) {
                        HashMap<String, Object> map1 = new HashMap<>();
                        map1.put("cId",courseBean.getData().get(position).getCId()) ;
                        map1.put("cName",courseBean.getData().get(position).getCName());
                        map1.put("cTime",courseBean.getData().get(position).getCTime());
                        HttpUtil.httpPost(BuildConfig.SELECTION_TEACHER, map1, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
                            @Override
                            public void success(HttpUtil.Result result) {
                                if (result.code == 200){
                                    CourseTchBean courseTchBean = new Gson().fromJson(result.content, CourseTchBean.class);
                                    SelectionTeacherFragment selectionTeacherFragment = new SelectionTeacherFragment(courseTchBean, new SelectionTeacherFragment.CallBack() {
                                        @Override
                                        public void callBack(boolean success,int p) {
                                            if (success){
                                                selection.setText(courseTchBean.getData().getTeachers().get(p).getTchName());
                                                selection.setTextColor(Color.rgb(0,0,0));
                                                selection.setEnabled(false);

                                            }
                                        }
                                    });

                                    selectionTeacherFragment.show(getSupportFragmentManager(),"TAG");
                                }else {
                                    CourseTchBean courseTchBean = new Gson().fromJson(result.content,CourseTchBean.class);
                                    selection.setText("不可选择");
                                    selection.setTextColor(Color.rgb(0,0,0));
                                    selection.setEnabled(false);
                                    Toast.makeText(CourseSelectionActivity.this, courseTchBean.getMessage(), Toast.LENGTH_SHORT).show();
                                }

                            }

                            @Override
                            public void failed(IOException e) {
                                Toast.makeText(CourseSelectionActivity.this, "网络连接异常!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }

            @Override
            public void failed(IOException e) {
                Toast.makeText(CourseSelectionActivity.this, "信息获取失败，请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        });
    }

}